<?php
namespace app\easytask\controller;

use cmf\controller\HomeBaseController;

use think\Db;
use think\db\Where;

class MobileController extends HomeBaseController
{


    public function MobileView()
    {
//        echo cmf_password('admin'); //通过sp_password密码设置为"admin"
//        exit;

        $category = $this->request->param('category');

        if($category == null)
        {
            $list = Db::name('receive')->where('finished',null)->select();
        }
        else
        {
            $list = Db::name('receive')->where('finished',null)->where('type',$category)->select();
        }


        $this->assign('list',$list);
        return $this->fetch('MobileView');
    }
    public function ReplyView()
    {

        $id = $this->request->param('receiveid');
        $result = Db::name('receive')->where('receive_id',$id)->find();
        Db::name('receive')->where('receive_id',$id)->update(['finished' => 1 ]);
        $this->assign('list', $result);

        return $this->fetch('ReplyView');
    }

    public function ReplyContent()
    {

        $receiveid = $this->request->param('receiveid');
        $backcontent = $this->request->param('back_content');
        $replykey = $this->request->param('reply_key');

        Db::name('receive')->where('receive_id',$receiveid)->update(['back_content' => $backcontent ]);
        Db::name('receive')->where('receive_id',$receiveid)->update(['finished' => 2 ]);
        $file_names = [];
        $data = $this->request->param();
        $count = 0;
        while(isset($data['file_urls'][$count])){
            $temp_name['url'] = $data['file_urls'][$count];
            $temp_name['name'] = $data['file_names'][$count];
            array_push($file_names,$temp_name);
            $count = $count+1;
        }
        $file_names = json_encode($file_names);
        $t = date('Y-m-d H:i:s');
        $data['single_finish_time']=$t;
        $data['file_names'] = $file_names;
        dump($data);
        unset($data['file_urls'],$data['receiveid']);

       Db::name('receive')->where('receive_id',$receiveid)->update($data);

        $this->success('回复成功',url('Mobile/MobileView'));
    }
    public  function RepliedView()
    {
        $result = Db::name('receive')->where('finished',2)->select();
        $this->assign('list',$result);
        return $this->fetch('RepliedView');
    }
    public function taskView()
    {
        return $this->fetch('task');
    }



    public function add_post(){
        if (IS_POST) {
            if(empty($_POST['term'])){
                $this->error("请至少选择一个分类！");
            }
            if(!empty($_POST['photos_alt']) && !empty($_POST['photos_url'])){
                foreach ($_POST['photos_url'] as $key=>$url){
                    $photourl=sp_asset_relative_url($url);
                    $_POST['smeta']['photo'][]=array("url"=>$photourl,"alt"=>$_POST['photos_alt'][$key]);
                }
            }
            $_POST['smeta']['thumb'] = sp_asset_relative_url($_POST['smeta']['thumb']);

            $_POST['post']['post_modified']=date("Y-m-d H:i:s",time());
            $_POST['post']['post_author']=get_current_admin_id();
            $article=I("post.post");
            $article['smeta']=json_encode($_POST['smeta']);
            $article['post_content']=htmlspecialchars_decode($article['post_content']);
            $result=$this->posts_model->add($article);
            if ($result) {
                foreach ($_POST['term'] as $mterm_id){
                    $this->term_relationships_model->add(array("term_id"=>intval($mterm_id),"object_id"=>$result));
                }

                $this->success("添加成功！");
            } else {
                $this->error("添加失败！");
            }
        }
    }

}